package work.xiaohong.websocket.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import work.xiaohong.common.constant.MessageStatus;
import work.xiaohong.common.constant.MessageType;
import work.xiaohong.common.eneity.PageResult;
import work.xiaohong.websocket.dao.SendMsgVoMapper;
import work.xiaohong.websocket.service.SendMsgVoService;
import work.xiaohong.websocket.vo.MsgListVo;
import work.xiaohong.websocket.vo.SendMsgVo;
import work.xiaohong.websocket.vo.UpdateMsgStatusVo;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaohong
 * @version 1.0
 * @date 2021/9/26 0026 11:17
 * @description
 */
@Service
public class SendMsgVoServiceImpl implements SendMsgVoService {

    @Autowired
    private SendMsgVoMapper sendMsgVoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 插入消息记录
     * @param sendMsgVo
     */
    @Override
    public void add(SendMsgVo sendMsgVo) {
        sendMsgVoMapper.save(sendMsgVo);
    }

    /**
     * 根据发送者分页查找聊天记录
     * @param msgListVo
     * @return
     */
    @Override
    public Page<SendMsgVo> findMsgListBySender(MsgListVo msgListVo) {
        return sendMsgVoMapper.findBySender(msgListVo.getSender(), PageRequest.of(msgListVo.getPage(),msgListVo.getSize()));
    }

    /**
     * 根据接收者分页查找聊天记录
     * @param msgListVo
     * @return
     */
    @Override
    public Page<SendMsgVo> findMsgListByReceiver(MsgListVo msgListVo) {
        return sendMsgVoMapper.findByReceiver(msgListVo.getReceiver(),PageRequest.of(msgListVo.getPage(),msgListVo.getSize()));
    }

    /**
     * 查找与某好友的聊天记录
     * @param msgListVo
     * @return
     */
    @Override
    public List<SendMsgVo> findMsgListBySenderAndReceiver(MsgListVo msgListVo) {
        // 查询发送的和接受的
        Criteria sendCriteria = new Criteria();
        sendCriteria.and("sender").is(msgListVo.getSender()).and("receiver").is(msgListVo.getReceiver());
        Criteria receiveCriteria = new Criteria();
        receiveCriteria.and("receiver").is(msgListVo.getSender()).and("sender").is(msgListVo.getReceiver());

        Criteria criteria = new Criteria();
        criteria.orOperator(sendCriteria,receiveCriteria);

        Query query = new Query(criteria);
//        query.skip(msgListVo.getPage()-1);
//        query.limit(msgListVo.getSize());
//        long count = mongoTemplate.count(query, SendMsgVo.class);
        List<SendMsgVo> msgListVos = mongoTemplate.find(query, SendMsgVo.class);
//        PageResult<List<SendMsgVo>> listPageResult = new PageResult<>(msgListVos, msgListVo.getPage(), msgListVo.getSize(), count);
        return msgListVos;
    }

    /**
     * 查找未读的聊天记录
     * @param msgListVo
     * @return
     */
    @Override
    public List<SendMsgVo> findUnReadMsgList(MsgListVo msgListVo) {
        Criteria criteria = new Criteria();
        criteria.and("sender").is(msgListVo.getSender());
        criteria.and("receiver").is(msgListVo.getReceiver());
        criteria.and("status").is(MessageStatus.SENDSUCCESS.getStatus());
        Query query = new Query(criteria);
        List<SendMsgVo> msgListVos = mongoTemplate.find(query, SendMsgVo.class);
        return msgListVos;
    }

    /**
     * 更新与某好友的未读聊天记录状态
     * @param updateMsgStatusVo
     */
    @Override
    public void updateMsgStatus(UpdateMsgStatusVo updateMsgStatusVo){
        //  查询条件

        Criteria criteria = new Criteria();
        criteria.and("receiver").is(updateMsgStatusVo.getUserId());
        criteria.and("sender").is(updateMsgStatusVo.getFriendId());

        criteria.and("label").in(Arrays.asList(updateMsgStatusVo.getLabels()));

        Query query = new Query(criteria);
        Update update = new Update();
        update.set("status",updateMsgStatusVo.getStatus ());

        mongoTemplate.updateMulti(query,update,SendMsgVo.class);
    }

    @Override
    public List<SendMsgVo> findAllUnReadMsgList(Long userId) {
        Criteria criteria = new Criteria();
        criteria.and("receiver").is(userId);
        criteria.and("status").is(MessageStatus.SENDSUCCESS.getStatus());
        Query query = new Query(criteria);
        List<SendMsgVo> msgListVos = mongoTemplate.find(query, SendMsgVo.class);
        return msgListVos;
    }
}
